<?php

namespace app\admin\controller;

use think\Controller;
use think\Cache;
use think\Db;
use think\Session;

class Func extends Controller
{
    /**
     * 修改密码
     * */
    public function updateAdminPwd()
    {
        if (request()->isPost()) {
            $input = request()->param();

            $id = session('id');
            $old_admin_pwd = $input['old_admin_pwd'];
            $new_admin_pwd = $input['new_admin_pwd'];

            if (empty($id)) apiReturn(2, '登录身份过期，请重新登录');
            if (empty($old_admin_pwd)) apiReturn(2, '请输入原密码');
            if (empty($new_admin_pwd)) apiReturn(2, '请输入新密码');

            $info = Db::name('user')->where(['id' => $id])->find();
            if ($info) {
                if (thinkDecrypt($info['password']) === $old_admin_pwd) {
                    $data = [
                        'password' => thinkEncrypt($new_admin_pwd),
                        'update_time' => time()
                    ];

                    $res = Db::name('user')->where(['id' => $id])->update($data);
                    if ($res) {
                        apiReturn(1, '修改密码成功');
                    } else {
                        apiReturn(2, '修改密码失败');
                    }
                } else {
                    apiReturn(2, '原密码错误，请重新输入');
                }
            } else {
                apiReturn(2, '当前账号信息不存在或已删除');
            }

        }
    }

    /**
     * 清理缓存
     * */
    public function clearCache()
    {
        Cache::clear();
        apiReturn(1, '清理缓存成功');
    }

    /**
     * 退出登录
     * */
    public function logout()
    {
        Session::clear();
        Cache::clear();

        adminReturn(1, '退出登录成功', url('/admin/login/login'));
    }


}
